home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / CUCD / PowerPC / vbcc / machines / amigawos / libsrc / stdio / fwrite.c < prev    next >
C/C++ Source or Header  |  1998-08-02  |  930b  |  47 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4.  
  5. size_t fwrite(void *ptr,size_t size,size_t nmemb,FILE *f)
  6. {
  7.   size_t total=size*nmemb;
  8.   char *p=ptr;
  9.   long result;
  10.  
  11.   if (!f) {
  12.     errno = EBADF;
  13.     return (0);
  14.   }
  15.   if ((f->flags&(_WRITEABLE|_READ|_ERR|_EOF))!=_WRITEABLE) {
  16.     errno = EBADF;
  17.     return (0);
  18.   }
  19.   f->flags |= _WRITE;
  20.   if (!f->bufsize)
  21.     f->bufsize = (f->flags&_UNBUF) ? 1 : BUFSIZ;
  22.   if (!f->base) {
  23.     if (!(f->base = malloc(f->bufsize+1))) {
  24.       errno = ENOMEM;
  25.       return (0);
  26.     }
  27.     f->base++;
  28.     f->pointer = f->base;
  29.     f->count = f->bufsize;
  30.   }
  31.   if (f->count==0)
  32.     f->count = f->bufsize;
  33.   if(total < f->count) {
  34.     memcpy(f->pointer,p,total);
  35.     f->pointer += total;
  36.     f->count -= total;
  37.     return (nmemb);
  38.   }
  39.   if (fflush(f))
  40.     return (0);
  41.   if ((result = _write(f->filehandle,p,total)) == EOF) {
  42.     f->flags|=_ERR;
  43.     return (0);
  44.   }
  45.   return (result/size);
  46. }
  47.